package com.cb.kfc.controller;

import com.cb.kfc.domain.Friend;
import com.cb.kfc.service.IFriendService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;
@Controller
@RequestMapping("/friend")
public class FriendController {

    @Autowired
    private IFriendService friendService;

    @RequestMapping("/findAll.do")
    public ModelAndView findAll() throws Exception {
        ModelAndView mv = new ModelAndView();
        List<Friend> friendList = friendService.findAll();
        mv.addObject("friendList", friendList);
        mv.setViewName("friend-list");
        return mv;
    }

    @RequestMapping("/save.do")
    public String save(Friend friend) throws Exception {
        friendService.save(friend);
        return "redirect:findAll.do";
    }

    /**
     * 删除
     * @return
     * @throws Exception
     */
    @RequestMapping("/delete.do")
    public String delete(String[] ids) throws Exception{
        Integer  integer = friendService.deleteFriendById(ids);
        return "redirect:findAll.do";
    }

    //查询单个
    @RequestMapping("/findOne.do")
    public ModelAndView findOne(String id) throws Exception {
        ModelAndView mv = new ModelAndView();
        Friend friend = friendService.findOne(id);
        mv.addObject("friend",friend);
        mv.setViewName("friend-update");
        return mv;
    }

    //会员类别更新
    @RequestMapping("/update.do")
    public String Update(Friend friend) throws Exception {
        friendService.update(friend);
        return "redirect:findAll.do";

    }

    //会员类别添加
    @RequestMapping("/friend-add.do")
    public String friendAdd() throws Exception {

         return "friend-add";

    }
}
